import { queryRegions, queryVehicleMap, queryAlarmType } from '@/api/crossRegionalMonitoring'
import { getStorageByKey, setStorageByKey } from '@/utils/public'

const state = {
  regions: getStorageByKey('regionsMap'),
  vehicleMap: getStorageByKey('vehicleMap')
}
const mutations = {
  SET_REGION: (state, regions) => {
    state.regions = regions
  },
  SET_VEHICLE: (state, vehicleMap) => {
    state.vehicleMap = vehicleMap
  }
}

const actions = {
  getRegions({ commit, state }) {
    return new Promise((resolve) => {
      queryRegions().then((res) => {
        const { data } = res
        const regionsList = data.data.bigAreaList || []
        commit('SET_REGION', regionsList)
        setStorageByKey('regionsMap', regionsList)
      }).then(() => {
        queryVehicleMap().then((res) => {
          const { data } = res
          const vehicleSeriesList = data.data.vehicleSeriesList || []
          commit('SET_VEHICLE', vehicleSeriesList)
          setStorageByKey('vehicleMap', vehicleSeriesList)
        })
      }).then(() => {
        queryAlarmType().then((res) => {
          const { data } = res
          const alarmTypeMap = data.data
          setStorageByKey('alarmTypeMap', alarmTypeMap)
        })
      })
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
